package com.edu.eduservice.controller;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.edu.common.utils.R;
import com.edu.eduservice.entity.EduCourse;
import com.edu.eduservice.entity.EduTeacher;
import com.edu.eduservice.service.EduCourseService;
import com.edu.eduservice.service.EduTeacherService;
import com.edu.eduservice.vo.TeacherQuery;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-05-27
 */
@Api(description = "讲师管理")
@RestController
@RequestMapping("/eduservice/teacher")
@CrossOrigin
public class EduTeacherController {
    @Autowired
    private EduTeacherService eduTeacherService;
    @Autowired
    private EduCourseService eduCourseService;


    //1,查询讲师所有数据
    @ApiOperation(value = "所有讲师列表")
    @GetMapping("/findAll")
    public R findAllTeacher(){
        List<EduTeacher> list = eduTeacherService.list(null);
        return R.ok().data("items",list);
    }

    //逻辑删除讲师
    @ApiOperation(value = "根据id删除讲师")
    @DeleteMapping("/deleteTeacherById/{id}")
    @Transactional
    public R deleteTeacherById(@ApiParam (name = "id",value = "讲师id", required = true)
                                   @PathVariable("id") String id){
        boolean b = eduTeacherService.removeById(id);
        if (b){
            return R.ok();
        }else {
            return R.error();
        }
    }

    //分页查询
    @ApiOperation(value = "分页查询讲师")
    @GetMapping("/pageList/{page}/{limit}")
    public R pageList(@ApiParam(name = "page" , value = "当前页码" ,required = true)
                          @PathVariable("page") Long page,
                      @ApiParam(name = "limit", value = "每页记录数",required = true)
                      @PathVariable("limit") Long limit){
        //创建分页
        Page<EduTeacher> pageParam = new Page<>(page,limit);
        //分页查询完 会封装到pageparam中
        eduTeacherService.page(pageParam,null);
        //获取查询到的数据
        List<EduTeacher> records = pageParam.getRecords();
        //获取总记录数
        long total = pageParam.getTotal();
        return R.ok().data("total",total).data("rows",records);

    }

//条件查询 带分页
    @PostMapping("/pageTeacherCondition/{page}/{limit}")
    @ApiOperation("分页所有讲师列表")
    public R pageQuery(@ApiParam(name = "当前页码",value = "page",required = true)
                        @PathVariable("page") Long page,
                       @ApiParam(name = "每页记录数",value = "limit",required = true)
                            @PathVariable("limit") Long limit,
                       @ApiParam(name = "查询对象",value = "teacherQuery",required = true)
                            @RequestBody(required = false) TeacherQuery teacherQuery){
        //创建分页对象
        Page<EduTeacher> pageParam = new Page<>(page,limit);


        //调用方法实现多条件分页查询

        eduTeacherService.pageQuery(pageParam,teacherQuery);


        //获取查询到的数据
        List<EduTeacher> records = pageParam.getRecords();//总页数

        long total = pageParam.getTotal();//总条数

        return R.ok().data("total",total).data("rows",records);
    }



//添加讲师
    @ApiOperation("新增讲师")
    @PostMapping
    @Transactional
    public R save(@ApiParam(name = "teacher",value = "讲师对象",required = true)
                  @RequestParam EduTeacher teacher){
            eduTeacherService.save(teacher);
            return R.ok();

    }



//根据id查询
    @ApiOperation(value = "根据id查询讲师")
    @GetMapping("/getTeacher/{id}")
    public R getById(@ApiParam(name = "id",value = "讲师id",required = true)
                     @PathVariable("id") String id){
        EduTeacher teacher = eduTeacherService.getById(id);
        return R.ok().data("teacher",teacher);
    }



//修改讲师
    @ApiOperation(value = "修改讲师")
    @PostMapping("/updateTeacher")
    @Transactional
    public R updateById(
            @ApiParam(name = "teacher", value = "teacher", required = true)
            @RequestBody EduTeacher teacher){

            eduTeacherService.updateById(teacher);
            return R.ok();
    }




//添加讲师
    @ApiOperation(value = "添加讲师")
    @PostMapping("/addTeacher")
    @Transactional
    public R addTeacher(@ApiParam(name = "teacher" ,value = "teacher",required = true)
                        @RequestBody EduTeacher teacher){
        boolean save = eduTeacherService.save(teacher);
        if (save){
            return R.ok().data("teacher",teacher);
        }else {
            return R.error();
        }
    }



    /**
     * 根据老师id查询课程
     *
     */
    @ApiOperation(value = "根据Id查询讲师")
    @GetMapping(value = "/{id}")
    public R getBytcId(
            @ApiParam(name = "id", value = "讲师ID", required = true)
            @PathVariable String id){
            //查询讲师信息
        EduTeacher teacher = eduTeacherService.getById(id);
        //根据讲师id查询这个讲师的课程列表
        List<EduCourse> eduCourses = eduCourseService.selectByTeacherId(id);
        return R.ok().data("course",eduCourses).data("teacher",teacher);
    }

}

